<?php

namespace App\Services;

use App\Models\WxMiniprogramConfig;
use App\Services\WechatApi\WechatApiBase;

class WxMiniprogramService extends ServiceBase
{
    public function generateUrlScheme($appid, $path, $query)
    {
        $config = WxMiniprogramConfig::where('appid', $appid)->first();
        if (!$config) {
            return;
        }

        $access_token = WechatService::getInstance()->getAccessToken($config->appid, $config->secret);
        $res = WechatApiBase::getInstance()->generateUrlScheme($access_token, $path, $query);
        if (!$res || $res['errcode'] !== 0) {
            return;
        }

        return $res['openlink'];
    }

    public function generateUrllink($appid, $path, $query)
    {
        $config = WxMiniprogramConfig::where('appid', $appid)->first();
        if (!$config) {
            return;
        }

        $access_token = WechatService::getInstance()->getAccessToken($config->appid, $config->secret);
        $res = WechatApiBase::getInstance()->generateUrllink($access_token, $path, $query);
        if (!$res || $res['errcode'] !== 0) {
            return;
        }

        return $res['openlink'];
    }
}
